//
//  BMRegisterController.m
//  特种车调度
//
//  Created by 陈宇 on 15/8/19.
//
//

#import "BMRegisterController.h"
#import "BMWebController.h"
#import "BMResetPasswordController.h"

@interface BMRegisterController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *agreeButton;

- (IBAction)argeeAction:(UIButton *)sender;
- (IBAction)codeAction:(UIButton *)sender;
- (IBAction)protocolAction:(UIButton *)sender;
- (IBAction)nextAction:(id)sender;
- (IBAction)directLoginAction:(id)sender;

@end

@implementation BMRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (IBAction)argeeAction:(UIButton *)sender {
    sender.selected = !sender.selected;
}

- (IBAction)codeAction:(UIButton *)sender {
    NSString *phone = _phoneTextField.text;
    if (![BMUtils isValidateMobile:phone]) {
        [self showHint:@"请输入有效手机号码!"];
        return;
    }
    BMRequest *request = [BMRequest requestWithPath:@"obtainVCode.htm"];
    request.params = @{@"mobile": phone, @"role": kRole};
    [self showHudWithhint:kDefaultMessage];
    [[BMHttpTool sharedInstance] postWith:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            __block int timeout = 60; //倒计时时间
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
            dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
            dispatch_source_set_event_handler(_timer, ^{
                if(timeout<=0){ //倒计时结束，关闭
                    dispatch_source_cancel(_timer);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //设置界面的按钮显示 根据自己需求设置
                        [sender setTitle:@"获取验证码" forState:UIControlStateNormal];
                        sender.userInteractionEnabled = YES;
                    });
                }else{
                    int seconds = timeout % 60;
                    NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //设置界面的按钮显示 根据自己需求设置
                        //NSLog(@"____%@",strTime);
                        [UIView beginAnimations:nil context:nil];
                        [UIView setAnimationDuration:1];
                        [sender setTitle:[NSString stringWithFormat:@"%@秒",strTime] forState:UIControlStateNormal];
                        [UIView commitAnimations];
                        sender.userInteractionEnabled = NO;
                    });
                    timeout--;
                }
            });
            dispatch_resume(_timer);
        } else {
            [self showHint:@"获取验证码失败,请重新获取!"];
        }
    }];
}

- (IBAction)protocolAction:(UIButton *)sender {
    BMWebController *webVC = [BMWebController new];
    webVC.type = 3;
//    webVC.path = [[NSBundle mainBundle] URLForResource:@"agree" withExtension:@"html"];
//    webVC.title = @"注册协议";
    [self showViewController:webVC sender:nil];
}

- (IBAction)nextAction:(id)sender {
    NSString *phone = _phoneTextField.text;
    if (![BMUtils isValidateMobile:phone]) {
        [self showHint:@"请输入规范的手机号!"];
        return;
    }
    if (_passwordTextField.text.length != 6) {
        [self showHint:@"请输入6位验证码!"];
        return;
    }
    if (!_agreeButton.isSelected) {
        [self showHint:@"需同意注册协议"];
        return;
    }
    BMRequest *request = [BMRequest requestWithPath:@"checkVCode.htm"];
    request.params = @{@"mobile": phone, @"vCode": _passwordTextField.text};
    [self showHudWithhint:kDefaultMessage];
    [[BMHttpTool sharedInstance] postWith:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            BMResetPasswordController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"resetPasswordVC"];
            vc.type = 1;
            vc.mobile = [response.rawResult objectForKey:@"phone"];
            [self showViewController:vc sender:nil];
        } else {
            [self showHint:@"验证码验证失败!"];
        }
    }];
}

- (IBAction)directLoginAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
